<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  
  <meta name="generator" content="Hugo 0.98.0" />

  
  <meta name="description" content="走在通往幸福的路上">
  

  
  <link rel="apple-touch-icon" sizes="180x180" href="https://blog.v5u.win/apple-touch-icon.png">

  
  <link rel="icon" type="image/png" sizes="32x32" href="https://blog.v5u.win/favicon-32x32.png">

  
  <link rel="icon" type="image/png" sizes="16x16" href="https://blog.v5u.win/favicon-16x16.png">

  
  <link rel="manifest" href="https://blog.v5u.win/site.webmanifest">

  
  <link rel="mask-icon" href="https://blog.v5u.win/safari-pinned-tab.svg" color="">

  <meta name="msapplication-TileColor" content="">

  <meta name="theme-color" content="">

  
  <link rel="stylesheet" href="https://blog.v5u.win/css/bootstrap.min.css" />

  
  <title>Go HTML template | 为吾优</title>
  

  <style>
body {
  min-width: 300px;
}

.custom-navbar {
  margin-bottom: 1em;
  height: 60px;
}

.custom-navbar a {
  display: inline-block; 
  padding: 18px 0;
  margin-right: 1em; 
  font-weight: bold; 
}

.custom-navbar a:hover,
.custom-navbar a:focus {
  text-decoration: none; 
}

@media print {
  .custom-navbar {
    display: none;
  }
}

article {
  padding-bottom: 1em;
}

img {
  max-width: 100%;
}


body {
  background-color: #fff;
}



body {
  color: #212529;
}



a {
  color: #007bff;
}



a:hover,
a:focus {
  color: #0056b3;
}



.custom-navbar {
  background-color: #212529;
}



.custom-navbar a {
  color: rgba(255,255,255,.75);
}



.custom-navbar a:hover,
.custom-navbar a:focus {
  color: rgba(255,255,255,1);
}



.container {
  max-width: 800px;
}





</style>
</head>

<body>
  <nav class="custom-navbar">
  <div class="container">
    
    <a href="/">文章</a>
    
    <a href="/tags/">标签</a>
    
    <a href="/about/">关于</a>
    
    <a href="/index.xml">RSS</a>
    
  </div>
</nav>
  
  <div class="container">
    <article>
      <h1>Go HTML template</h1>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">文档</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">go</span> <span style="color:#a6e22e">doc</span> <span style="color:#a6e22e">text</span><span style="color:#f92672">/</span><span style="color:#a6e22e">template</span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">$</span> <span style="color:#66d9ef">go</span> <span style="color:#a6e22e">doc</span> <span style="color:#a6e22e">html</span><span style="color:#f92672">/</span><span style="color:#a6e22e">template</span>
</span></span></code></pre></div><h3 id="应用">应用</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// 定义模板显示格式
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	<span style="color:#66d9ef">const</span> <span style="color:#a6e22e">templ</span> = <span style="color:#e6db74">`&lt;p&gt;A: </span><span style="color:#75715e">{{</span><span style="color:#a6e22e">.A</span><span style="color:#75715e">}}</span><span style="color:#e6db74">&lt;/p&gt;&lt;p&gt;B: </span><span style="color:#75715e">{{</span><span style="color:#a6e22e">.B</span><span style="color:#75715e">}}</span><span style="color:#e6db74">&lt;/p&gt;`</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// 模板配置函数
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	<span style="color:#a6e22e">t</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">template</span>.<span style="color:#a6e22e">Must</span>(<span style="color:#a6e22e">template</span>.<span style="color:#a6e22e">New</span>(<span style="color:#e6db74">&#34;escape&#34;</span>).<span style="color:#a6e22e">Parse</span>(<span style="color:#a6e22e">templ</span>))
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">data</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">A</span> <span style="color:#66d9ef">string</span>        <span style="color:#75715e">// untrusted plain text
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>		<span style="color:#a6e22e">B</span> <span style="color:#a6e22e">template</span>.<span style="color:#a6e22e">HTML</span> <span style="color:#75715e">// trusted HTML
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	}
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// A是一个普通字符串，B是一个信任的template.HTML字符串类型。
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	<span style="color:#a6e22e">data</span>.<span style="color:#a6e22e">A</span> = <span style="color:#e6db74">&#34;&lt;b&gt;Hello!&lt;/b&gt;&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">data</span>.<span style="color:#a6e22e">B</span> = <span style="color:#e6db74">&#34;&lt;b&gt;Hello!&lt;/b&gt;&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// 使用定义好的 模板 输出到 控制台
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">t</span>.<span style="color:#a6e22e">Execute</span>(<span style="color:#a6e22e">os</span>.<span style="color:#a6e22e">Stdout</span>, <span style="color:#a6e22e">data</span>); <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Fatal</span>(<span style="color:#a6e22e">err</span>)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">autoescape.html 结果
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">A:&lt;b&gt;Hello!&lt;/b&gt;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">B:Hello!
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span></code></pre></div><h3 id="语法">语法</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#75715e">//取值
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>&lt;<span style="color:#a6e22e">p</span>&gt;{{.<span style="color:#a6e22e">ArticleContent</span>}}&lt;<span style="color:#a6e22e">span</span>&gt;{{.<span style="color:#a6e22e">ArticleId</span>}}&lt;<span style="color:#f92672">/</span><span style="color:#a6e22e">span</span>&gt;&lt;<span style="color:#f92672">/</span><span style="color:#a6e22e">p</span>&gt;
</span></span><span style="display:flex;"><span><span style="color:#75715e">// if 语句
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>{{<span style="color:#66d9ef">if</span> .<span style="color:#a6e22e">condition</span>}}
</span></span><span style="display:flex;"><span>{{<span style="color:#a6e22e">end</span>}}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>{{<span style="color:#66d9ef">if</span> .<span style="color:#a6e22e">condition1</span>}}
</span></span><span style="display:flex;"><span>{{<span style="color:#66d9ef">else</span> <span style="color:#66d9ef">if</span> .<span style="color:#a6e22e">contition2</span>}}
</span></span><span style="display:flex;"><span>{{<span style="color:#a6e22e">end</span>}}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// go 方法调用
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>{{<span style="color:#a6e22e">funcname</span> .<span style="color:#a6e22e">arg1</span> .<span style="color:#a6e22e">arg2</span>}}
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">add</span>(<span style="color:#a6e22e">left</span> <span style="color:#66d9ef">int</span>, <span style="color:#a6e22e">right</span> <span style="color:#66d9ef">int</span>) <span style="color:#66d9ef">int</span>
</span></span><span style="display:flex;"><span>{{<span style="color:#a6e22e">add</span> <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span>}}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 逻辑判断
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">not</span> <span style="color:#a6e22e">非</span>
</span></span><span style="display:flex;"><span>{{<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">not</span> .<span style="color:#a6e22e">condition</span>}}
</span></span><span style="display:flex;"><span>{{<span style="color:#a6e22e">end</span>}}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">and</span> <span style="color:#a6e22e">与</span>
</span></span><span style="display:flex;"><span>{{<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">and</span> .<span style="color:#a6e22e">condition1</span> .<span style="color:#a6e22e">condition2</span>}}
</span></span><span style="display:flex;"><span>{{<span style="color:#a6e22e">end</span>}}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">or</span> <span style="color:#a6e22e">或</span>
</span></span><span style="display:flex;"><span>{{<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">or</span> .<span style="color:#a6e22e">condition1</span> .<span style="color:#a6e22e">condition2</span>}}
</span></span><span style="display:flex;"><span>{{<span style="color:#a6e22e">end</span>}}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">eq</span> <span style="color:#a6e22e">等于</span>
</span></span><span style="display:flex;"><span>{{<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">eq</span> .<span style="color:#a6e22e">var1</span> .<span style="color:#a6e22e">var2</span>}}
</span></span><span style="display:flex;"><span>{{<span style="color:#a6e22e">end</span>}}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">ne</span> <span style="color:#a6e22e">不等于</span>
</span></span><span style="display:flex;"><span>{{<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">ne</span> .<span style="color:#a6e22e">var1</span> .<span style="color:#a6e22e">var2</span>}}
</span></span><span style="display:flex;"><span>{{<span style="color:#a6e22e">end</span>}}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">lt</span> <span style="color:#a6e22e">小于</span> (<span style="color:#a6e22e">less</span> <span style="color:#a6e22e">than</span>)
</span></span><span style="display:flex;"><span>{{<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">lt</span> .<span style="color:#a6e22e">var1</span> .<span style="color:#a6e22e">var2</span>}}
</span></span><span style="display:flex;"><span>{{<span style="color:#a6e22e">end</span>}}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">le</span> <span style="color:#a6e22e">小于等于</span>
</span></span><span style="display:flex;"><span>{{<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">le</span> .<span style="color:#a6e22e">var1</span> .<span style="color:#a6e22e">var2</span>}}
</span></span><span style="display:flex;"><span>{{<span style="color:#a6e22e">end</span>}}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">gt</span> <span style="color:#a6e22e">大于</span>
</span></span><span style="display:flex;"><span>{{<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">gt</span> .<span style="color:#a6e22e">var1</span> .<span style="color:#a6e22e">var2</span>}}
</span></span><span style="display:flex;"><span>{{<span style="color:#a6e22e">end</span>}}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">ge</span> <span style="color:#a6e22e">大于等于</span>
</span></span><span style="display:flex;"><span>{{<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">ge</span> .<span style="color:#a6e22e">var1</span> .<span style="color:#a6e22e">var2</span>}}
</span></span><span style="display:flex;"><span>{{<span style="color:#a6e22e">end</span>}}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">循环</span>
</span></span><span style="display:flex;"><span>{{<span style="color:#66d9ef">range</span> <span style="color:#960050;background-color:#1e0010">$</span><span style="color:#a6e22e">i</span>, <span style="color:#960050;background-color:#1e0010">$</span><span style="color:#a6e22e">v</span> <span style="color:#f92672">:=</span> .<span style="color:#a6e22e">slice</span>}}
</span></span><span style="display:flex;"><span>{{<span style="color:#a6e22e">end</span>}}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>{{<span style="color:#66d9ef">range</span> .<span style="color:#a6e22e">slice</span>}}
</span></span><span style="display:flex;"><span>{{.<span style="color:#a6e22e">field</span>}}
</span></span><span style="display:flex;"><span>{{<span style="color:#a6e22e">end</span>}}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">模板嵌套</span>
</span></span><span style="display:flex;"><span>{{<span style="color:#a6e22e">template</span> <span style="color:#e6db74">&#34;navbar&#34;</span>}}<span style="color:#75715e">// 使用
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>{{<span style="color:#a6e22e">define</span> <span style="color:#e6db74">&#34;navbar&#34;</span>}}<span style="color:#75715e">// 定义
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>{{<span style="color:#a6e22e">end</span>}}
</span></span><span style="display:flex;"><span>{{<span style="color:#a6e22e">template</span> <span style="color:#e6db74">&#34;navbar&#34;</span> .}}<span style="color:#75715e">// 获取父模板的变量
</span></span></span></code></pre></div><p>参考https://www.kancloud.cn/cserli/golang/531904</p>

    </article>
  </div>

  
  
  

  
</body>

</html>